<?php

namespace App\Lib;

use App\Dep\SystemDep;
use GuzzleHttp\Client;
use GuzzleHttp\Psr7\Request;

class JdWebSdk
{
    public $cookie;

    public $client;

    public function __construct($cookie)
    {
        $this->cookie = $cookie;
        $this->client = new Client();
    }

    public function getVideoUploadUrl($videoName, $videoFile)
    {
        log_daily('jd_web_sdk')->info("request param getVideoUploadUrl {$videoName}");;
        $body = [
            "videoName" => $videoName,
            "fileSize" => strlen($videoFile),
            "style" => 2,
        ];

        $headers = [
            'Referer' => 'https://dr.jd.com/',
            'Cookie' => $this->cookie,
            'User-Agent' => 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36'
        ];
        $options = [
            'form_params' => [
                'functionId' => 'videoQueryUploadUrl',
                'appid' => 'dr-platform',
                'client' => 'pc',
                'loginType' => '3',
                'body' => json_encode($body)
            ],
            "verify" => false
        ];
        $request = new Request('POST', 'https://api.m.jd.com/videoQueryUploadUrl', $headers);
        $res = $this->client->sendAsync($request, $options)->wait();
        log_daily('jd_web_sdk')->info("response getVideoUploadUrl {$res->getBody()}");
        return json_decode($res->getBody(), true);
    }

    public function uploadVideo($param)
    {
        $systemDep = new SystemDep();
        $resSystem = $systemDep->first();

        log_daily('jd_web_sdk')->info("request param", $param);;
        $headers = [
            'Referer' => 'https://dr.jd.com/',
            'Cookie' => $this->cookie,
            'User-Agent' => 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36'
        ];
        $body = json_encode($param);
        $options = [
            'verify' => false,
            'form_params' => [
                'functionId' => 'articlePublishVideo',
                'appid' => 'dr-platform',
                'client' => 'pc',
                'loginType' => '3',
                'body' => $body,
                'h5st'=> $resSystem->jingdong_h5
            ]];

        $request = new Request('POST', 'https://api.m.jd.com/articlePublishVideo', $headers);
        $res = $this->client->sendAsync($request, $options)->wait();
        log_daily('jd_web_sdk')->info("response: {$res->getBody()}");;
        return json_decode($res->getBody(), true);
    }

    public function uploadImage($resImage, $fileName)
    {

        $headers = [
            'Origin' => 'https://dr.jd.com/',
            'Referer' => 'https://dr.jd.com/',
            'User-Agent' => 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36',
            'p' => '',
            'Cookie' => $this->cookie
        ];
        $options = [
            "verify" => false,
            'multipart' => [
                [
                    'name' => 'Filedata',
                    'contents' => $resImage,
                    'filename' => $fileName,
                ],
//                [
//                    'name' => 'width',
//                    'contents' => '800'
//                ],
//                [
//                    'name' => 'height',
//                    'contents' => '800'
//                ]
            ]];
        $request = new Request('POST', 'https://plat-gw.jd.com/daren/fileupload/image', $headers);
        $res = $this->client->sendAsync($request, $options)->wait();
        return json_decode($res->getBody(), true);
    }

    public function getTopicList($page, $title = "", $skuIds = [])
    {
        $param = [
            "page" => $page,
            "title" => $title,
            "skuIds" => $skuIds
        ];
        log_daily('jd_web_sdk')->info("request param", $param);;
        $headers = [
            'Origin' => 'https://dr.jd.com/',
            'Referer' => 'https://dr.jd.com/',
            'Cookie' => $this->cookie,
            'User-Agent' => 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36'
        ];
        $body = json_encode($param);
        $options = [
            'verify' => false,
            'form_params' => [
                'functionId' => 'topicContributeQuery4Page',
                'appid' => 'dr-platform',
                'client' => 'pc',
                'loginType' => '3',
                'body' => $body
            ]];

        $request = new Request('POST', 'https://api.m.jd.com/topic/contributeQuery4Page', $headers);
        $res = $this->client->sendAsync($request, $options)->wait();
        log_daily('jd_web_sdk')->info("response: {$res->getBody()}");;
        return json_decode($res->getBody(), true);
    }

    public function getTopicDetail($topicId)
    {
        $param = [
            "subjectId" => $topicId
        ];
        log_daily('jd_web_sdk')->info("request param", $param);

        $headers = [
            'Referer' => 'https://dr.jd.com/',
            'Cookie' => $this->cookie,
            'User-Agent' => 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36'
        ];
        $body = json_encode($param);
        $options = [
            'verify' => false,
            'form_params' => [
                'functionId' => 'topicQueryDetail',
                'appid' => 'dr-platform',
                'client' => 'pc',
                'loginType' => '3',
                'body' => $body
            ]];

        $request = new Request('POST', 'https://api.m.jd.com/topicQueryDetail', $headers);
        $res = $this->client->sendAsync($request, $options)->wait();
        log_daily('jd_web_sdk')->info("response: {$res->getBody()}");;
        return json_decode($res->getBody(), true);
    }


    public function getInterestTagGroup()
    {
        log_daily('jd_web_sdk')->info("request param");;
        $headers = [
            'Origin' => 'https://dr.jd.com/',
            'Referer' => 'https://dr.jd.com/',
            'Cookie' => $this->cookie,
            'User-Agent' => 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36'
        ];

        $options = [
            'verify' => false,
            'form_params' => [
                'functionId' => 'nhTagQuery2And3Classify',
                'appid' => 'dr-platform',
                'client' => 'pc',
                'loginType' => '3',
            ]];

        $request = new Request('POST', 'https://api.m.jd.com/nhTagQuery2And3Classify', $headers);
        $res = $this->client->sendAsync($request, $options)->wait();
        log_daily('jd_web_sdk')->info("response: {$res->getBody()}");;
        $resArr = json_decode($res->getBody(), true);
        $resGroup = [];
        foreach ($resArr['result'] as $key => $value) {
            if ($value['id'] == 632) {
                $resGroup = $value['children'];
            }
        }
        return $resGroup;
    }


    public function getTagGroup()
    {
        log_daily('jd_web_sdk')->info("request param");;
        $headers = [
            'Origin' => 'https://dr.jd.com/',
            'Referer' => 'https://dr.jd.com/',
            'Cookie' => $this->cookie,
            'User-Agent' => 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36'
        ];

        $options = [
            'verify' => false,
            'form_params' => [
                'functionId' => 'nhTagQuery2And3Classify',
                'appid' => 'dr-platform',
                'client' => 'pc',
                'loginType' => '3',
            ]];

        $request = new Request('POST', 'https://api.m.jd.com/nhTagQuery2And3Classify', $headers);
        $res = $this->client->sendAsync($request, $options)->wait();
        log_daily('jd_web_sdk')->info("response: {$res->getBody()}");;
        return json_decode($res->getBody(), true);
    }

    public function getTag($parentId)
    {
        log_daily('jd_web_sdk')->info("request param {$parentId}");;
        $param = [
            "thirdClassifyId" => $parentId
        ];
        $headers = [
            'Origin' => 'https://dr.jd.com/',
            'Referer' => 'https://dr.jd.com/',
            'Cookie' => $this->cookie,
            'User-Agent' => 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36'
        ];

        $options = [
            'verify' => false,
            'form_params' => [
                'functionId' => 'nhTagQueryBy3ClassifyId',
                'appid' => 'dr-platform',
                'client' => 'pc',
                'loginType' => '3',
                'body' => json_encode($param)
            ]];

        $request = new Request('POST', 'https://api.m.jd.com/nhTagQueryBy3ClassifyId', $headers);
        $res = $this->client->sendAsync($request, $options)->wait();
        log_daily('jd_web_sdk')->info("response: {$res->getBody()}");;
        return json_decode($res->getBody(), true);
    }

    public function getGenreTag()
    {

    }

//    public function syncStatus($contentId){
//
//        log_daily('jd_web_sdk')->info("request param {$contentId}");;
//
//        $headers = [
//            'Origin' => 'https://dr.jd.com/',
//            'Referer' => 'https://dr.jd.com/',
//            'Cookie' => $this->cookie,
//            'User-Agent' => 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36'
//        ];
//
//        $options = [
//            'verify' => false,
//            'form_params' => [
//                "id" => $contentId,
//                "subposition" => 143,
//                "title" => "",
//                "status" => "",
//                "queryType" => 2,
//                "pageFirstId" => "",
//                "pageLatId" => "",
//                "isEnd" => 1
//            ]];
//
//        $request = new Request('POST', 'https://plat-gw.jd.com/daren/article/query', $headers);
//        $res = $this->client->sendAsync($request, $options)->wait();
//        log_daily('jd_web_sdk')->info("response: {$res->getBody()}");;
//        return  json_decode($res->getBody(), true);
//
//    }


    public function syncStatus($contentId)
    {

        log_daily('jd_web_sdk')->info("request param {$contentId}");;

        $param = [
            "id" => $contentId,
            "title" => "",
            "contentStatus" => "",
            "style" => "11",
            "subPosition" => "",
            "startPublishTime" => "",
            "endPublishTime" => "",
            "queryType" => 2,
            "cursor" => ""
        ];

        $headers = [
            'Origin' => 'https://dr.jd.com/',
            'Referer' => 'https://dr.jd.com/',
            'Cookie' => $this->cookie,
            'User-Agent' => 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36'
        ];

        $options = [
            'verify' => false,
            'form_params' => [
                "functionId" => "contentArticleList",
                "appid" => "dr-platform",
                "client" => "pc",
                "loginType" => 3,
                "body" => json_encode($param)
            ]];

        $request = new Request('POST', 'https://api.m.jd.com/contentArticleList', $headers);
        $res = $this->client->sendAsync($request, $options)->wait();

        log_daily('jd_web_sdk')->info("response: {$res->getBody()}");;

        return json_decode($res->getBody(), true);

    }
}
